package com.glc.hms.system.controller.web;


import com.glc.hms.common.controller.BaseController;
import com.glc.hms.common.data.ResponseData;
import com.glc.hms.common.data.SuccessResponseData;
import com.glc.hms.common.tree.MenuTreeInfo;
import com.glc.hms.core.annotation.SysLogAnno;
import com.glc.hms.core.common.ConstantFactory;
import com.glc.hms.core.shiro.ShiroUtil;
import com.glc.hms.system.entity.SysMenu;
import com.glc.hms.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 菜单信息表 前端控制器
 * </p>
 *
 * @author Santa
 * @since 2019-11-26
 */
@Slf4j
@Api(value = "菜单", description = "菜单接口", basePath = "/webApi/menu")
@RestController
@RequestMapping("/webApi/menu")
public class MenuController extends BaseController {


    @Autowired
    private ISysMenuService menuService;

    /**
     * 添加菜单
     */
    @ApiOperation(value = "添加菜单", response = ResponseData.class)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SysLogAnno(value = "添加菜单")
    //@Permission({"系统管理员"})
    public ResponseData addMenu(SysMenu menu) {
        menuService.save(menu);
        return SUCCESS_TIP;
    }

    /**
     * 更新菜单
     */
    @ApiOperation(value = "更新菜单", response = ResponseData.class)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @SysLogAnno(value = "更新菜单")
    //@Permission({"系统管理员"})
    public ResponseData updateMenu(SysMenu menu) {
        menuService.updateById(menu);
        return SUCCESS_TIP;
    }


    /**
     * 获取菜单列表
     */
    @ApiOperation(value = "获取菜单列表", response = ResponseData.class)
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    //@Permission({"系统管理员"})
    public ResponseData getMenu(@ApiParam(name = "roleId", value = "角色id", required = false) Long roleId) {
        //获取用户角色
        //TODO 暂时不考虑多角色的问题
        if(StringUtils.isEmpty(roleId)) {
            roleId = ShiroUtil.getUserNotNull().getRoleId();
        }
        //List<MenuTreeInfo> menuTreeInfos = menuService.queryMenuList(roleId);
        List<MenuTreeInfo> menuTreeInfos = ConstantFactory.me().getMenuListByRoleId(roleId);
        return new SuccessResponseData(menuTreeInfos);
    }



	
}
